package com.ugoodtech.mdcc.web.service.impl;

import cn.hutool.core.date.DateUtil;
import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_Complete;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_NeedFollowUp;
import com.ugoodtech.mdcc.core.domain.VstarUserKpiReport.VstarUserKpiReport_Plan;
import com.ugoodtech.mdcc.core.helper.SearchVoId;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.DepartmentCoreService;
import com.ugoodtech.mdcc.core.service.SearchPermissionsService;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.WeekServiceReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WeekServiceReportServiceImpl implements WeekServiceReportService {

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;

    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private CustomerRegularReportRepository customerRegularReportRepository;

    @Autowired
    private DepartmentCoreService departmentCoreService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private SearchPermissionsService searchPermissionsService;


    @Override
    public VstarUserKpiReport provinceWeekReport(Long userId, Long stationId) {
        return null;
    }

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);

    /**
     * 获取技术人员kpi报告--改
     *
     * @return
     */
    @Override
    public JsonResponse statWeekReport(Long loginUserId, String userName, String stationName, String proinces, String brandname, String category, Long startTime, Long endTime, Long serviceStartTime, Long serviceEndTime,
                                       Long customerStat, Long customertype, String paintBrand, String productPosition, String stationNum, Long serviceType, Boolean isFollowUp,
                                       Long departmentId, Pageable pageable) {
        User user = userRepository.findOne(loginUserId);
        UserProfile userProfile = userProfileRepository.findByUserId(loginUserId);
        SearchVoId searchVoId;
        if (user.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(userProfile, proinces, brandname);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(userProfile, proinces, brandname);
        }
        if (searchVoId.getResultStationId().isEmpty()) {
            return JsonResponse.successResponseWithData(new VstarUserKpiReport());
        }
        QCustomerRegularReport qCustomerRegularReport = QCustomerRegularReport.customerRegularReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qCustomerRegularReport.deleted.eq(false));
//        builder.and(qCustomerRegularReport.enabled.eq(true));
        builder.and(qCustomerRegularReport.regularReport.station.id.in(searchVoId.getResultStationId()));
        if (StringUtils.isNotEmpty(stationNum)) {
            builder.and(qCustomerRegularReport.regularReport.station.stationNum.like("%" + stationNum + "%"));
        }
        if (customerStat != null && customerStat != 0) {
            builder.and(qCustomerRegularReport.regularReport.station.customerStat.eq(customerStat));
        }
        if (customertype != null && customertype != 0) {
            builder.and(qCustomerRegularReport.regularReport.station.customerType.eq(customertype));
        }
        if (StringUtils.isNotEmpty(paintBrand)) {
            builder.and(qCustomerRegularReport.regularReport.station.paintBrand.like("%" + paintBrand + "%"));
        }
        if (StringUtils.isNotEmpty(productPosition)) {
            builder.and(qCustomerRegularReport.regularReport.station.productPosition.like("%" + productPosition + "%"));
        }
        if (StringUtils.isNotEmpty(category)) {
            builder.and(qCustomerRegularReport.regularReport.station.category.eq(category));
        }
        if (StringUtils.isNotEmpty(userName) || departmentId != null && departmentId != 0) {
            List<Long> parementDeparetment = departmentCoreService.parentDepartment();
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            List<Long> idList = new ArrayList<>();

            if (null != departmentId) {
                Department department = departmentRepository.findOne(departmentId);
                if (department != null) {
                    if (parementDeparetment.contains(departmentId)) {
                        List<Long> lowerDepartment = departmentCoreService.getLowerDepartment(departmentId);
                        idList.addAll(lowerDepartment);
                    } else {
                        idList.add(departmentId);
                    }
                }
                booleanBuilder.and(qUserProfile.departmentList.any().id.in(idList));
            }
            if (StringUtils.isNotEmpty(userName)) {
                booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            }
            List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
            idList = new ArrayList<>();
            for (UserProfile searchUserProfile : userProfileList) {
                idList.add(searchUserProfile.getUser().getId());
            }
            builder.and(qCustomerRegularReport.regularReport.user.id.in(idList));
        }

        if (StringUtils.isNotEmpty(stationName)) {
            builder.and(qCustomerRegularReport.regularReport.station.name.like("%" + stationName + "%"));
        }
        if (serviceStartTime != null && serviceStartTime != 0 && serviceEndTime != null && serviceEndTime != 0) {
            builder.and(qCustomerRegularReport.regularReport.startServiceTime.after(new Date(serviceStartTime)));
            builder.and(qCustomerRegularReport.regularReport.endServiceTime.before(new Date(serviceEndTime)));
        }
        if (startTime != null && startTime != 0 && endTime != null && endTime != 0) {
            builder.and(qCustomerRegularReport.regularReport.creationTime.after(new Date(startTime)));
            builder.and(qCustomerRegularReport.regularReport.creationTime.before(new Date(endTime)));
        }
        if (serviceType != null && serviceType != 0) {
            builder.and(qCustomerRegularReport.regularReport.serviceTypAnswerIds.like("%" + serviceType + "%"));
        }
        if (isFollowUp != null) {
            builder.and(qCustomerRegularReport.regularReport.deparmentIds.ne(""));
            if (isFollowUp == true) {
                builder.and(qCustomerRegularReport.regularReport.deparmentIds.isNotNull());
            } else {
                builder.and(qCustomerRegularReport.regularReport.deparmentIds.isNull());
            }
        }
        Sort sort = new Sort(Sort.Direction.DESC, "regularReport.creationTime");
        List<CustomerRegularReport> customerRegularReportList = (List<CustomerRegularReport>) customerRegularReportRepository.findAll(builder, sort);
        List<StationReport> stationReportPage = new ArrayList<>();
        for (CustomerRegularReport customerRegularReport : customerRegularReportList) {
            stationReportPage.add(customerRegularReport.getRegularReport());
        }
        VstarUserKpiReport vstarUserKpiReport = new VstarUserKpiReport();

        List<VstarUserKpiReport_Complete> vstarUserKpiReport_complete = new ArrayList<>();
        List<VstarUserKpiReport_NeedFollowUp> vstarUserKpiReport_needFollowUp = new ArrayList<>();
        List<VstarUserKpiReport_Plan> vstarUserKpiReport_plan = new ArrayList<>();
        for (StationReport stationReport : stationReportPage) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);
            if (answer != null) {
                //获取其他信息
                VstarUserKpiReport_Complete complete = new VstarUserKpiReport_Complete();
                UserProfile workUser = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (workUser != null && StringUtils.isNotEmpty(workUser.getRealName())) {
                    complete.setWorkUser(workUser.getRealName());
                } else {
                    complete.setWorkUser("N/A");
                }
                complete.setBrand(stationReport.getStation().getBrand());
                //设置报告ID
                complete.setReportId(stationReport.getId());
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            complete.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        complete.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            complete.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        complete.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append("其他(" + stationColorReport.getAnswerOther() + ")" + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    complete.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    complete.setProduct_position(stringBuffer.toString());
                }
                //具体品牌
                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 8L);
                if (null != stationColorReport) {
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String brandId : stationColorReport.getAnswerId().split(",")) {
                        if (brandId.equals("369")) {
                            if (StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                stringBuffer.append("其他(" + stationColorReport.getAnswerOther() + "),");
                            }
                        }
                        questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(brandId));
                        if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                            stringBuffer.append(questionAnswer.getName() + ",");
                        }
                    }
                    complete.setBrand(stringBuffer.toString());
                }

                complete.setProvince(stationReport.getStation().getProvince());
                complete.setTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 155L);
                try {
                    String[] targeIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targeIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("191")) {
                                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                    stringBuffer.append("其他(" + regularServiceReport.getAnswerOther() + ")" + ",");
                                }
                            } else if (id.equals("189")) {
                                if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                    stringBuffer.append(stationReport.getAnswerOther() + ",");
                                } else {
                                    stringBuffer.append("未填写内容,请联系此工作人员");
                                }
                            } else {
                                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                    stringBuffer.append(answerOption.getName() + ",");
                                }
                            }
                        }
                    }
                    complete.setWorkType(stringBuffer.toString());
                } catch (Exception e) {
                    complete.setWorkType("N/A");
                }
                complete.setCustomerName(stationReport.getStation().getName());
                complete.setCategory(stationReport.getStation().getCategory());
                complete.setStationNum(stationReport.getStation().getStationNum());
                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    complete.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    complete.setServiceCompany("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    Station station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        complete.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        complete.setAddress(province + city);
                    }
                } else {
                    complete.setAddress("");
                }

                //获取服务自我评价
                try {
                    regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 157L);
                    complete.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    complete.setServicePoint("N/A");
                }
                vstarUserKpiReport_complete.add(complete);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_complete(vstarUserKpiReport_complete);

        for (StationReport stationReport : stationReportPage) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 181L);
            StationRegularServiceReport answer1 = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 354L);
            if (answer == null && answer1 == null) {
                VstarUserKpiReport_NeedFollowUp needFollowUp = new VstarUserKpiReport_NeedFollowUp();
                UserProfile workUser = userProfileRepository.findByUserId(stationReport.getUser().getId());
                if (workUser != null && StringUtils.isNotEmpty(workUser.getRealName())) {
                    needFollowUp.setWorkUser(workUser.getRealName());
                } else {
                    needFollowUp.setWorkUser("N/A");
                }
                needFollowUp.setProvince(stationReport.getStation().getProvince());
                needFollowUp.setCategory(stationReport.getStation().getCategory());
                needFollowUp.setStationNum(stationReport.getStation().getStationNum());
                needFollowUp.setBrand(stationReport.getStation().getBrand());
                //设置报告ID
                needFollowUp.setReportId(stationReport.getId());
                //设置具体品牌
                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 8L);
                if (null != stationColorReport) {
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String brandId : stationColorReport.getAnswerId().split(",")) {
                        if (brandId.equals("369")) {
                            if (StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                stringBuffer.append("其他(" + stationColorReport.getAnswerOther() + "),");
                            }
                        }
                        QuestionAnswerOption questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(brandId));
                        if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                            stringBuffer.append(questionAnswer.getName() + ",");
                        }
                    }
                    needFollowUp.setBrand(stringBuffer.toString());
                }
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            needFollowUp.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        needFollowUp.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            needFollowUp.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        needFollowUp.setCustomerStat(questionAnswer.getName());
                    }
                }
                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append("其他(" + stationColorReport.getAnswerOther() + ")" + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    needFollowUp.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    needFollowUp.setProduct_position(stringBuffer.toString());
                }

                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 156L);
                if (regularServiceReport != null) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(regularServiceReport.getAnswerId()));
                    List<String> departmentList = new ArrayList<>();
                    departmentList.add(answerOption.getName());
                    if (answerOption.getId().equals(182)) {
                        StationReport deparmentReport = regularServiceReport.getStationReport();
                        String[] departmentIds = deparmentReport.getDeparmentIds().split(",");
                        for (String id : departmentIds) {
                            if (StringUtils.isEmpty(id)) {
                                continue;
                            }
                            Department department = departmentRepository.findOne(Long.valueOf(id));
                            departmentList.add(department.getTotalName());
                        }
                    }
                    needFollowUp.setAbnormalSituation(departmentList);
                }

                regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                needFollowUp.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                if (regularServiceReport == null) {
                    needFollowUp.setCuntomerName("N/A");
                    needFollowUp.setWorkType("N/A");
                } else {
                    String[] targerIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targerIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("191")) {
                                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                    stringBuffer.append("其他(" + regularServiceReport.getAnswerOther() + ")" + ",");
                                }
                            } else if (id.equals("189")) {
                                if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                    stringBuffer.append(stationReport.getAnswerOther() + ",");
                                } else {
                                    stringBuffer.append("未填写内容,请联系此工作人员");
                                }
                            } else {
                                QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswerOption != null&&questionAnswerOption.getName()!=null) {
                                    String targer = questionAnswerOption.getName();
                                    stringBuffer.append(targer + ",");
                                }
                            }
                        }
                    }
                    needFollowUp.setCuntomerName(stationReport.getStation().getName());
                    needFollowUp.setWorkType(stringBuffer.toString());
                }
                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    needFollowUp.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    needFollowUp.setServiceCompany("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    Station station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        needFollowUp.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        needFollowUp.setAddress(province + city);
                    }
                } else {
                    needFollowUp.setAddress("");
                }


                //获取服务自我评价
                try {
                    regularServiceReport = stationRegularServiceReportRepository.selectQuestion(stationReport.getId(), 157L);
                    needFollowUp.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    needFollowUp.setServicePoint("N/A");
                }
                vstarUserKpiReport_needFollowUp.add(needFollowUp);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_needFollowUp(vstarUserKpiReport_needFollowUp);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
        Station station = null;
        for (StationReport stationReport : stationReportPage) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 354L);
            if (answer != null) {
                VstarUserKpiReport_Plan plan = new VstarUserKpiReport_Plan();
                plan.setProvice(stationReport.getStation().getProvince());
                plan.setDateTime(simpleDateFormat.format(stationReport.getUpdateTime()));
                plan.setCategory(stationReport.getStation().getCategory());
                plan.setStationNum(stationReport.getStation().getStationNum());
                plan.setStartTime(stationReport.getServiceStartTime());
                plan.setEndTime(stationReport.getServiceEndTime());
                //设置报告ID
                plan.setReportId(stationReport.getId());
                //设置客户类型
                QuestionAnswerOption questionAnswer = null;
                if (stationReport.getStation().getCustomerType() != null && stationReport.getStation().getCustomerType() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 366L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 185L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            plan.setCustomerType(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerType());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        plan.setCustomerType(questionAnswer.getName());
                    }
                }
                //设置客户状态
                if (stationReport.getStation().getCustomerStat() != null && stationReport.getStation().getCustomerStat() != 0) {
                    if (stationReport.getStation().getCustomerStat() == 362L) {
                        StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 184L);
                        if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                            plan.setCustomerStat(stationColorReport.getAnswerOther());
                        }
                    }
                    questionAnswer = questionAnswerOptionRepository.findOne(stationReport.getStation().getCustomerStat());
                    if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                        plan.setCustomerStat(questionAnswer.getName());
                    }
                }
                //具体品牌
                StationColorReport stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 8L);
                if (null != stationColorReport) {
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String brandId : stationColorReport.getAnswerId().split(",")) {
                        if (brandId.equals("369")) {
                            if (StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                stringBuffer.append("其他(" + stationColorReport.getAnswerOther() + "),");
                            }
                        }
                        questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(brandId));
                        if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                            stringBuffer.append(questionAnswer.getName() + ",");
                        }
                    }
                    plan.setBrand(stringBuffer.toString());
                }

                //设置油漆品牌
                if (stationReport.getStation().getPaintBrand() != null && StringUtils.isNotEmpty(stationReport.getStation().getPaintBrand())) {
                    String[] paintBrnadIds = stationReport.getStation().getPaintBrand().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : paintBrnadIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            if (id.equals("280")) {
                                stationColorReport = stationColorReportRepository.selectReport(stationReport.getId(), 13L);
                                if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                                    stringBuffer.append("其他(" + stationColorReport.getAnswerOther() + ")" + ",");
                                }
                            } else {
                                questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                                if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                    stringBuffer.append(questionAnswer.getName() + ",");
                                }
                            }
                        }
                    }
                    plan.setPaintBrand(stringBuffer.toString());
                }
                //设置品牌定位
                if (stationReport.getStation().getProductPosition() != null && StringUtils.isNotEmpty(stationReport.getStation().getProductPosition())) {
                    String[] productIds = stationReport.getStation().getProductPosition().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : productIds) {
                        if (StringUtils.isNotEmpty(id)) {
                            questionAnswer = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (questionAnswer != null && StringUtils.isNotEmpty(questionAnswer.getName())) {
                                stringBuffer.append(questionAnswer.getName() + ",");
                            }
                        }
                    }
                    plan.setProduct_position(stringBuffer.toString());
                }
                //封装工作人员
                if (stationReport.getUser() != null) {
                    UserProfile profile = userProfileRepository.findByUserId(stationReport.getUser().getId());
                    if (profile != null && StringUtils.isNotEmpty(profile.getRealName())) {
                        plan.setWorkUser(profile.getRealName());
                    } else {
                        plan.setWorkUser("N/A");
                    }
                } else {
                    plan.setWorkUser("N/A");
                }

                //封装工作地点
                if (stationReport.getStation() != null) {
                    station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getAddress())) {
                        plan.setAddress(station.getAddress());
                    } else {
                        String province = "";
                        String city = "";
                        if (StringUtils.isNotEmpty(station.getProvince())) {
                            province = station.getProvince();
                        }
                        if (StringUtils.isNotEmpty(station.getCity())) {
                            city = station.getCity();
                        }
                        plan.setAddress(province + city);
                    }
                } else {
                    plan.setAddress("");
                }

                //封装客户名称
                if (stationReport.getStation() != null) {
                    station = stationReport.getStation();
                    if (StringUtils.isNotEmpty(station.getName())) {
                        plan.setCustomerUser(station.getName());
                    } else {
                        plan.setCustomerUser("N/A");
                    }
                } else {
                    plan.setCustomerUser("N/A");
                }

                //获取油漆服务商
                QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
                builder = new BooleanBuilder();
                builder.and(qStationColorReport.deleted.eq(false));
                builder.and(qStationColorReport.enabled.eq(true));
                builder.and(qStationColorReport.questionId.eq(14L));
                builder.and(qStationColorReport.stationReport.station.id.eq(stationReport.getStation().getId()));
                StationColorReport colorReport = stationColorReportRepository.findOne(builder);
                try {
                    plan.setServiceCompany(colorReport.getAnswerOther());
                } catch (Exception e) {
                    plan.setServiceCompany("N/A");
                }

                //获取服务自我评价
                try {
                    StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 157L);
                    plan.setServicePoint(regularServiceReport.getAnswerOther());
                } catch (Exception e) {
                    plan.setServicePoint("N/A");
                }
                StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), 155L);
                if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerId())) {
                    String[] targeIds = regularServiceReport.getAnswerId().split(",");
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (String id : targeIds) {
                        if (id.equals("191")) {
                            if (regularServiceReport != null && StringUtils.isNotEmpty(regularServiceReport.getAnswerOther())) {
                                stringBuffer.append("其他(" + regularServiceReport.getAnswerOther() + "),");
                            }
                        } else if (id.equals("189")) {
                            if (StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
                                stringBuffer.append("其他(" + stationReport.getAnswerOther() + ")" + ",");
                            } else {
                                stringBuffer.append("未填写内容,请联系此工作人员");
                            }
                        } else {
                            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                            if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                                stringBuffer.append(answerOption.getName() + ",");
                            }
                        }
                    }
                    plan.setWorkType(stringBuffer.toString());
                } else {
                    plan.setWorkType("");
                }
                vstarUserKpiReport_plan.add(plan);
            }
        }
        vstarUserKpiReport.setVstarUserKpiReport_Plan(vstarUserKpiReport_plan);
//        List<VstarUserKpiReport> reportList = new ArrayList<>();
//        reportList.add(vstarUserKpiReport);
//        return JsonResponse.successResponseWithPageData(new PageImpl(reportList, pageable, stationReportPage.getTotalElements()));
        return JsonResponse.successResponseWithData(vstarUserKpiReport);
    }


    private String getProvinceStr(List<Nation> nations) {
        StringBuffer stringBuffer = new StringBuffer("");
        for (Nation nation : nations) {
            stringBuffer.append(nation.getProvince() + ",");
        }
        return stringBuffer.toString();
    }


}
